package Previo;

import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;

public class ProcedimientosDAO {
	
	private Conexion con = new Conexion ();
	
	public ProcedimientosDAO (){
		
	}
	
	public Object [] obtenerProcedures (){
		PreparedStatement stmt = null;
		ResultSet rs = null;
		Object [] procedures  =new Object [10]; 
		//ArrayList <String> procedures = new ArrayList<String>();
		con.conectar();
		try {
			stmt = con.getCon().prepareStatement(Conexion.getSPS());
			rs= stmt.executeQuery();
			int i= 0;
			while (rs.next()){
				procedures[i++]=rs.getString(1);
			}
			return procedures;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public Object [][] obtenerResultados (String procedure, int zona, int categoria){
		//Object [][] resultados= new Object [50][50];
		CallableStatement stm= null;
		ResultSet rs= null;
		con.conectar();
		try {
			int i=0;
			Vector f = new Vector();
			stm= con.getCon().prepareCall("{call "+procedure+ "(?,?)}");
			stm.setInt(1, zona);
			stm.setInt(2, categoria);
			rs= stm.executeQuery();
			ResultSetMetaData rsm = rs.getMetaData();
			while (rs.next()) {
				i++;
				Vector c= new Vector();
				f.add(c);
				for (int j = 1; j <= rs.getMetaData().getColumnCount(); j++) {
					c.add(rs.getString(j));
					//resp[i - 1][j - 1] = rs.getString(j);
				}
			}
			Object[][] resp = new Object[f.size()][rs.getMetaData().getColumnCount()];
			for (int ii = 0; ii < f.size(); ii++)
				for (int jj = 0; jj < rs.getMetaData().getColumnCount(); jj++)
					resp[ii][jj] = ((Vector)f.elementAt(ii)).elementAt(jj);
			return resp;
		}catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public Object [] obtenerColumnas (String procedure, int zona, int categoria){
		CallableStatement stm= null;
		ResultSet rs= null;
		con.conectar();
		try {
			
			stm= con.getCon().prepareCall("{call "+procedure+ "(?,?)}");
			stm.setInt(1, zona);
			stm.setInt(2, categoria);
			rs= stm.executeQuery();
			ResultSetMetaData rsm = rs.getMetaData();
			Object [] columnas = new Object [rsm.getColumnCount()];
			for (int i=1; i<rsm.getColumnCount(); i++)
				columnas[i]= rsm.getColumnName(i);
			return columnas;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

}
